/// <reference path="../Resources/js/jquery/jquery.debug.js" />
/// <reference path="../Controls/Items/MItem.js" />
/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("BM");

// TabButtons object

BM.TabButtons = function(node) {
    BM.TabButtons.initializeBase(this, []);
    this.node = node;
}

BM.TabButtons.prototype = {
    getNode: function() {
        return this.node;
    },

    getItem: function() {
        return this.item;
    },

    setItem: function(item) {
        this.item = item;
    },

    clear: function(item) {
        this.setItem(item);
        $(this.getNode()).children().remove();
    },

    getDepth: function() {
        if (this.depth == null) {
            this.depth = this.getNode().getAttribute('depth');
        }
        return this.depth;
    },

    setDepth: function(value) {
        this.depth = value;
    },
    
    resetDepth: function() {
        this.depth = null;
    },

    isCountEmptyContainers: function() {
        if (this.countEmptyContainers == null) {
            this.countEmptyContainers = (this.getNode().getAttribute('countEmptyContainers') == 'True');
        }
        return this.countEmptyContainers;
    },

    update: function(item, clickEvent) {
        if (item == this.getItem()) return;
        this.clear(item);
        this.appendAll(item);
    },

    appendAll: function(item) {
        var depth = 0;
        while (depth < this.getDepth()) {
            if (!item) {
                break;
            }
            if (this.append(item)) {
                depth++;
            }
            item = item.getParentItem();
        }
    },

    append: function(item) {
        var buttons = $(item.node).children('.BC').children().clone();
        for (var i = 0; i < buttons.length; i++) {
            buttons[i].id += '2';
            this.getNode().appendChild(buttons[i]);
        }
        return (buttons.length > 0 || this.isCountEmptyContainers());
    }
}

BM.TabButtons.get = function() {
    if (BM.TabButtons._instance == null) {
        BM.TabButtons._instance = new BM.TabButtons(BM.TabButtons.getNode());
    }
    return BM.TabButtons._instance;
}

BM.TabButtons.getNode = function() {
    return document.getElementById("__TabMenu");
}